//
//  _iOSUtils_UIViewAddtions.m
//  iOSUtils
//
//  Created by HJC on 11-9-8.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "_iOSUtils_UIViewAddtions.h"
#import <QuartzCore/QuartzCore.h>



NSInteger UIViewAutoresizingKeepInMiddle =   
UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin |
UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;



@implementation UIView(_iOSUtils_Addtions)


- (UIView*)  viewIsKindOf:(Class)cls recursive:(BOOL)recursive
{
    for (UIView* aView in self.subviews)
    {
        if ([aView isKindOfClass:cls])
        {
            return aView;
        }
    }
    
    if (recursive)
    {
        for (UIView* aView in self.subviews)
        {
            UIView* findView = [aView viewIsKindOf:cls recursive:YES];
            if (findView)
            {
                return findView;
            }
        }
    }
    return nil;
}


- (UIView*) viewIsKindOf:(Class)cls
{
    return [self viewIsKindOf:cls recursive:YES];
}



- (UIImage*) renderToImage
{
    UIGraphicsBeginImageContext(self.bounds.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}


+ (UIView*) loadFromNibNamed:(NSString*)name isKindOf:(Class)cls
{
    NSArray* array = [[NSBundle mainBundle] loadNibNamed:name owner:nil options:0];
    for (NSObject* object in array)
    {
        if ([object isKindOfClass:cls])
        {
            return (UIView*)object;
        }
    }
    return nil;
}


+ (UIView*) loadFromNibNamed:(NSString*)name
{
    return [self loadFromNibNamed:name isKindOf:[self class]];
}

@end





@implementation UIWebView(_iOSUtils_Addtions)

- (void) hideWebViewShadow
{
    UIView* scrollView = [self viewIsKindOf:[UIScrollView class]];
    UIView* imageView = [scrollView viewIsKindOf:[UIImageView class]];
    imageView.hidden = YES;
}

@end